﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TowerBase : MonoBehaviour {
    public Tower GetTower;
    public Vector3 offest = Vector3.zero;
    public bool HasTower
    {
        get { return GetTower != null; }
    }
    public virtual void CreatTower(TowerType towerType)
    {
        GameObject obj = null;

        
        switch (towerType)
        {
            case TowerType.None:
                break;
            case TowerType.Archer:
                obj = Resources.Load<GameObject>("Tower/Archer/war_tower_top_1_1");
                break;
            case TowerType.Mage:
                obj = Resources.Load<GameObject>("Tower/Mage/mage_tower_top_1_1");
                break;
            case TowerType.War:
                obj = Resources.Load<GameObject>("Tower/War/war_tower_top_4_1");
                break;
            case TowerType.Turret:
                obj = Resources.Load<GameObject>("Tower/Turret/war_tower_top_3_1");
                break;
        }
        if (obj == null)
            return;
        Debug.Log("创建塔");
        Tower  t= obj.GetComponent<Tower>();
        if(GameManager.Instance.UseCoin(t.price)){
            GameObject o= Instantiate(obj, Cameracontroller.currSelectObj.transform.position + offest, Quaternion.identity);
            GetTower = o.GetComponent<Tower>();
            GameManager.Instance.AddTower(o);
            Debug.Log("使用金币");
        }
        else{
            Destroy(obj);
        }
        

    }
    public void SellTower()
    {
        GetTower.Sell();
        EffectController.Instance.attackRangeEffect.SetActive(false);
        UIMainMeun.Instance.towerInfo_View.Hide();
        GetTower = null;
    }

    public void UpTower()
    {
        if (!GameManager.Instance.UseCoin(GetTower.upPrice))
        {
            return;
        }
        GameObject obj = GetTower.Up();
        if (obj)
        {
            GameManager.Instance.RemoveTower(GetTower.gameObject);
            Destroy(GetTower.gameObject);
            GameObject o= Instantiate(obj,transform.position+ offest, Quaternion.identity);
            GetTower = o.GetComponent<Tower>();
            GameManager.Instance.AddTower(o);

        }
    }

    
}
